package com.yc.shadowdemo;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Creator: yc
 * Date: 2021/5/28 10:31
 * UseDes:
 */

public class YcFileUtils {
    /**
     * SD卡根目录地址
     */
    public final static String SD_PATH = Environment.getExternalStorageDirectory().getPath();

    public static boolean copyFile(Context context, String assetsFilePath, String path) {
        try {
            File file = YcFileUtils.createFile(path);
            InputStream inputStream = context.getAssets().open(assetsFilePath);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int byteRead;
            while (-1 != (byteRead = inputStream.read(buffer))) {
                fileOutputStream.write(buffer, 0, byteRead);
            }
            inputStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean copy2(Context context, String assetsFilePath, String path) {
        try {
            String[] fileNames = context.getAssets().list("temp");
            if (fileNames == null || fileNames.length <= 0) {
                return false;
            }
            File file;
            for (String fileName : fileNames) {
                if (TextUtils.isEmpty(path)) {
                    path = YcFileUtils.SD_PATH + File.separator + "zhao_pian" + File.separator + fileName;
                }
                file = YcFileUtils.createFile(path);
                InputStream inputStream = context.getAssets().open(assetsFilePath);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int byteRead;
                while (-1 != (byteRead = inputStream.read(buffer))) {
                    fileOutputStream.write(buffer, 0, byteRead);
                }
                inputStream.close();
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static File createFile(String filePath) {
        File newFile = null;
        try {
            newFile = new File(filePath);
            //判断文件是否不存在，不存在则创建
            if (!newFile.getParentFile().exists()) {
                if (!newFile.getParentFile().mkdirs()) {
                    Log.e("YcUtils", "文件夹创建失败" + filePath);
                }
            }
            if (newFile.exists()) {
                newFile.delete();
            }
            if (newFile.createNewFile()) {
                Log.e("YcUtils", "文件创建成功" + filePath);
            } else {
                Log.e("YcUtils", "文件创建失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("YcUtils", "文件创建失败异常:" + e.getMessage());
        }
        return newFile;
    }
}
